package poker.com;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class poker extends Activity {
	/** Called when the activity is first created. */
	
	Button btnCrono,btnPlay,btnPause,btnStop;
	CheckBox chScreen;
	int min,sec,mino =2,seco=0;
	Handler mHandler= new Handler();
	Runnable servi;
	Thread t;
	boolean continuar = false,pause=false;
	TextView tv;
	CheckBox.OnCheckedChangeListener change;
	Typeface face;
	MediaPlayer mp;
	static final int TIME_DIALOG_ID = 0;
	private TextToSpeech mTts;
	String voz;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.only_time);
		
		
		
		
		btnCrono = (Button)findViewById(R.id.btnCrono);
		btnPlay = (Button)findViewById(R.id.btnPlay);
		btnPause = (Button)findViewById(R.id.btnPause);
		btnStop = (Button)findViewById(R.id.btnStop);
		chScreen = (CheckBox)findViewById(R.id.chScreen);
		tv=(TextView)findViewById(R.id.barCrono);
		mp = MediaPlayer.create(poker.this, R.drawable.beep);
		
		min = mino;
		sec = seco;
		actualizarCrono();
		
		//cambiamos la fuente
		
		face=Typeface.createFromAsset(getAssets(), "fonts/whatrg.ttf");
		tv.setTypeface(face);
		face=Typeface.createFromAsset(getAssets(), "fonts/digital.ttf");
		btnCrono.setTypeface(face);
		
		change = new CheckBox.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if(chScreen.isChecked()){
					//mantener pantalla encendida
					getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
					Toast.makeText(poker.this,R.string.msg_keep_screen, Toast.LENGTH_SHORT).show();
				}else{
					//permitir que se apague la pantalla
					getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
				}
			}
			
		};
		
		
		chScreen.setOnCheckedChangeListener(change);
	}//fin constructor

	public void cronoPlay(View view){
		
		if(continuar == true){
			
		}else{
			continuar = true;
			pause = false;
			miHilo();
			t.start();
		}
	}
	
	public void cronoPause(View view){
		continuar = false;
		pause = true;
		
	}
	
	public void cronoStop(View view){
		
		continuar = false;
		pause = false;
		min = mino;
		sec = seco;
		actualizarCrono();
	}
	

	protected void miHilo(){
		t= new Thread(){
			public void run(){
				while(continuar){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(continuar==true){
						mHandler.post(ejecutarAccion);
					}
					
				}
			}
		};
	}
	final Runnable ejecutarAccion = new Runnable(){
		public void run(){
			//Toast.makeText(poker.this, "Esto es una prueba", Toast.LENGTH_SHORT).show();
			if(sec>0){
				sec=sec-1;
				actualizarCrono();
				if(min==0&&sec<10){
					mp.start();
				}
				if(min==0&&sec==0){
					voz = "Las ciegas han cambiado!";
					onActivityResult();
				}
			}else{
				if(min>0){
					sec = 59;
					min = min-1;
					actualizarCrono();
					if(min==0){
						//mp.start();
						voz = "Queda un minuto para que suban las ciegas!";
						onActivityResult();
					}
				}else{
					sec = 0;
					actualizarCrono();
					//mp.start();
					min = mino;
					sec = seco;
					actualizarCrono();
					
				}
				
				
			}
		}
	};
	
	public boolean onKeyDown(int keyCode, KeyEvent  event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
        	if(continuar==true||pause == true){
        		mensaje();
        	}else{
        		finish();
        	}
        }
        return true;
	}
	
	public void actualizarCrono(){
		if(min<10){
			if(sec<10){
				btnCrono.setText("0"+min+":0"+sec);
			}else{
				btnCrono.setText("0"+min+":"+sec);
			}
		}else{
			if(sec<10){
				btnCrono.setText(min+":0"+sec);
			}else{
				btnCrono.setText(min+":"+sec);
			}
		}
	}
	
	public void mensaje(){
	
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setMessage("En tiempo esta en marcha, ¿esta seguro de cancelarlo?") // Esto determina la pregunta de la ventana
		.setCancelable(false)
		.setPositiveButton("Aceptar",new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int id) {

		// Aqui ponemos el codigo a ejecutar
		// al pulsar el boton ÔøΩguardarÔøΩ
			continuar=false;
			finish();
		}
		})
		.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int id) {

		// Aqui ponemos el codigo a ejecutar
		// al pulsar el boton ÔøΩCancelarÔøΩ

        	Toast.makeText(poker.this, "Operación Cancelada", Toast.LENGTH_SHORT).show();
		}
		});
		AlertDialog alert = alt_bld.create();
		alert.setTitle("Poker Blind"); // Aqui ponemos el titulo de la ventana
		alert.setIcon(R.drawable.icon); // Aqui ponemos el icono de la ventana
		alert.show();
		
	}
	
	public void cronometro(View view){
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		final EditText input = new EditText(this);
		
		alert.setView(input);
		input.setText(Integer.toString(mino));
		input.selectAll();
		alert.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString().trim();
				
					mino = Integer.parseInt(value);
				if(mino >= 0 && mino <=90){
					min = mino;
					actualizarCrono();
				}else{
					Toast.makeText(poker.this, "Número incorrecto", Toast.LENGTH_SHORT).show();
				}
				
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
		alert.show();

	}
	
	protected void onActivityResult(){
		mTts = new TextToSpeech(this, new OnInitListener(){

			@Override
			public void onInit(int status) {
				// TODO Auto-generated method stub
				if (status == TextToSpeech.SUCCESS){
					
					Locale loc = new Locale("es", "","");
					mTts.setLanguage(loc);
				mTts.speak(voz,TextToSpeech.QUEUE_ADD, null);
				}
			}
			
		});
		
		
	}
	

}